package co.fitstart.admin.entity.search;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.course.round.OnlineRoundGroupStatus;
import co.fitstart.entity.course.round.RoundStatus;


public class OnlineRoundSearch implements QueryBuilder{
    
    private boolean grouped;
    
    private String group;
    
    private String code;
    
    private String name;

    private String mobile;
    
    private Boolean investigated;
    
    private Boolean qrcodeStatus;
    
    private List<Long> roundIds;
    
    private Order order;

    private Filter filter;
    
    /**
     * default constructor
     */
    public OnlineRoundSearch() {
        
        this.order = new Order().add("round.onlineRoundAttr.qrcodeStatus", "ASC");
        
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        if(filter == null || refresh) {
            
            filter =  new Filter().addEquals("round.code", code)
                                  .addEquals("round.courseStatus", RoundStatus.PREPARE)
                                  .addEquals("round.onlineRoundAttr.qrcodeStatus", qrcodeStatus)
                                  .addLike("round.student.name", name)
                                  .addLike("round.student.mobile", mobile);
            
            if(grouped && StringUtils.isEmpty(group)) {
                Map<String, Object> parameter = new HashMap<String, Object>();
                parameter.put("groupStatus", new OnlineRoundGroupStatus[]{OnlineRoundGroupStatus.DISALLOW_GROUP, OnlineRoundGroupStatus.UNGROUPED});
                filter.addSentence("round.onlineRoundAttr.onlineRoundGroupStatus in (:groupStatus)", parameter);
            }
            
            Map<String, Object> parameters = new HashMap<String, Object>();
            if(investigated != null) {
                if(investigated) {
                    parameters.put("groupStatus", new OnlineRoundGroupStatus[]{OnlineRoundGroupStatus.GROUPING, OnlineRoundGroupStatus.GROUPED});
                } else {
                    parameters.put("groupStatus", new OnlineRoundGroupStatus[]{OnlineRoundGroupStatus.DISALLOW_GROUP});
                }
                filter.addSentence("round.onlineRoundAttr.onlineRoundGroupStatus in (:groupStatus)", parameters);
            }
            
            if(StringUtils.isNotEmpty(group) && roundIds !=null && !roundIds.isEmpty()) {
                Map<String, Object> parameter = new HashMap<String, Object>();
                parameter.put("roundIds", roundIds);
                filter.addSentence("round.id in ( :roundIds )", parameter);
            }
            
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM CourseRound round WHERE round.disabled = false " + getPreparedFilter(false).getClauseSentence() + getOrder().getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }
    
    /**
     * 
     * @param roundIds
     * @return
     */
    public OnlineRoundSearch editRoundIds(List<Long> roundIds) {
        this.roundIds = roundIds;
        return this;
    }

    /**
     * @return the grouped
     */
    public boolean isGrouped() {
        return grouped;
    }

    /**
     * @return the group
     */
    public String getGroup() {
        return group;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the investigated
     */
    public Boolean getInvestigated() {
        return investigated;
    }

    /**
     * @return the qrcodeStatus
     */
    public Boolean getQrcodeStatus() {
        return qrcodeStatus;
    }

    /**
     * @return the roundIds
     */
    public List<Long> getRoundIds() {
        return roundIds;
    }

    /**
     * @param grouped the grouped to set
     */
    public void setGrouped(boolean grouped) {
        this.grouped = grouped;
    }

    /**
     * @param group the group to set
     */
    public void setGroup(String group) {
        this.group = group;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param investigated the investigated to set
     */
    public void setInvestigated(Boolean investigated) {
        this.investigated = investigated;
    }

    /**
     * @param qrcodeStatus the qrcodeStatus to set
     */
    public void setQrcodeStatus(Boolean qrcodeStatus) {
        this.qrcodeStatus = qrcodeStatus;
    }

    /**
     * @param roundIds the roundIds to set
     */
    public void setRoundIds(List<Long> roundIds) {
        this.roundIds = roundIds;
    }
    
}
